import dva, { DvaOption } from "dva";
import router from "./router";
import "./mock/index";
import global from "./model/global";
import { IGlobalState } from "./model/type";
import "antd/dist/reset.css";
import "./index.css";

/**
 * 初始化dva state的方法
 * @returns
 */

const initialGlobalState = () => {
  const globalLocal = JSON.parse(localStorage.getItem("global") || "{}");
  return {
    ...global.state,
    ...globalLocal,
  };
};
const app = dva({
  /**
   * 每次改变时state都会出发
   * 每次改变就可以存储到loaclStorage里面
   * 在初始胡的时候，再拿出来
   */
  onStateChange(state: { global: IGlobalState }) {
    localStorage.setItem("global", JSON.stringify(state.global));
  },
  initialState: {
    global: initialGlobalState(),
  },
} as unknown as DvaOption);

app.model(global);

app.router(router);
//挂载路由

app.start("#root");
